package com.zz.ecommerce.reportStat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by fanshilei on 2015/12/18.
 */
@Service
public class StsAnalysisService {

    @Autowired
    private ReportStatDao reportStatDao;

    /**
     * 经营实体统计分析数据-企业类型数量统计图
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<StsAnalysisBean> qryEntStsChartB() {
        return reportStatDao.qryEntStsChartB();
    }

    /**
     * 经营实体统计分析数据-各直属局备案量统计图
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<StsAnalysisBean> qryEntStsChartC() {
        return reportStatDao.qryEntStsChartC();
    }

    /**
     * 商品备案统计分析数据-备案量折线图
     *
     * @return
     */
    @Transactional(readOnly = true)
    public Map<String, List<StsAnalysisBean>> qryEntStsChartE() {
        Map<String, List<StsAnalysisBean>> chartMap = new HashMap<String, List<StsAnalysisBean>>();
        chartMap.put("PL", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_PL")));
        chartMap.put("CBE", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_CBE")));
        chartMap.put("LST", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_LST")));
        chartMap.put("FOREIGN_LST", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_FOREIGN_LST")));
        chartMap.put("PAY", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_PAY")));
        chartMap.put("STORE", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_STORE")));
        chartMap.put("AGENT", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_AGENT")));
        chartMap.put("INSP", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_INSP")));
        chartMap.put("CUST", getStsAnalysisBeans(reportStatDao.qryEntStsChartE("DECL_TYPE_CUST")));
        return chartMap;
    }

    private List<StsAnalysisBean> getStsAnalysisBeans(List<StsAnalysisBean> list) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        List<StsAnalysisBean> newList = new ArrayList<StsAnalysisBean>();
        for (int i = 0; i < 12; i++) {
            boolean equal = true;
            calendar.setTime(new Date());
            calendar.add(Calendar.MONTH, -i);
            for (StsAnalysisBean stsAnalysisBean : list) {
                if (df.format(calendar.getTime()).equals(stsAnalysisBean.getName())) {
                    newList.add(stsAnalysisBean);
                    equal = false;
                    break;
                }
            }
            if (equal) {
                newList.add(new StsAnalysisBean(df.format(calendar.getTime()), 0));
            }
        }
        return newList;
    }

    /**
     * 经营实体统计分析数据-近一年内备案经营主体趋势图
     *
     * @return
     */
    @Transactional(readOnly = true)
    public List<StsAnalysisBean> qryEntStsChartD() {
        return getStsAnalysisBeans(reportStatDao.qryEntStsChartD());
    }

}
